from flask import Blueprint

from py.Services.SummaryService import SummaryService
from py.commonResponse.CommonResponse import *

bp_record = Blueprint('record', __name__, url_prefix="/records")  # 蓝图使用方法，参数里给定文件名，还可以给定url前缀


#  查找某人借阅过的书目
@bp_record.route('/book/<int:readerID>', methods=['GET'])
def record_QueryBook_ByReaderID(readerID):
    recordList = SummaryService.Record_QueryBook_ByReaderID(readerID)
    recordList1 = []
    for record in recordList:
        recordList1.append(dict(record))
    if not recordList1:
        return createForSuccessWM("该用户没有任何借阅书目记录")
    return createForSuccessWMD('查找成功', recordList1)


#  查找某人借阅历史
@bp_record.route('<int:readerID>', methods=['GET'])
def record_QueryHistory_ByReaderID(readerID):
    recordList = SummaryService.Record_QueryHistory_ByReaderID(readerID)
    recordList1 = []
    for record in recordList:
        recordList1.append(dict(record))
    if not recordList1:
        return createForSuccessWM("该用户没有任何借阅记录")
    return createForSuccessWMD('查找成功', recordList1)


#  查找某书被借阅历史
@bp_record.route('bookRecords/<int:ISBN>', methods=['GET'])
def recordBook_QueryHistory_ByISBN(ISBN):
    recordList = SummaryService.RecordBook_QueryHistory_ByISBN(ISBN)
    if recordList is None:
        return '这本书不存在或者没有被借阅过'
    recordList1 = []
    for record in recordList:
        recordList1.append(dict(record))
    if not recordList1:
        return createForSuccessWM("该书没有任何被借阅记录")
    return createForSuccessWMD('查找成功', recordList1)


# 查询某人当前的借阅书目
@bp_record.route('<int:readerID>/now', methods=['GET'])
def recordNow_QueryBook_ByReaderID(readerID):
    recordList = SummaryService.RecordNow_QueryBook_ByReaderID(readerID)
    recordList1 = []
    for record in recordList:
        recordList1.append(dict(record))
    if not recordList1:
        return createForSuccessWM("该用户现在没有任何借阅")
    return createForSuccessWMD('查找成功', recordList1)


#  查找所有借阅历史
@bp_record.route('', methods=['GET'])
def record_Query_All():
    recordList = SummaryService.Record_Query_All()
    recordList1 = []
    for record in recordList:
        recordList1.append(dict(record))
    if not recordList1:
        return createForSuccessWM("没有任何借阅书目记录")
    return createForSuccessWMD('查找成功', recordList1)
